﻿var directory = "";
var resultlist = [];
var lasterrorcode = 0;
var result = {
    init: 0,
    getlocation: 0,
    setlocation: 0,
    getsuspenddata: 0,
    setsuspenddata: 0,
    getstatus: 0,
    setstatus: 0,
    getscore: 0,
    setscore: 0,
    gettotaltime: 0,
    getprogress: 0,
    setsessiontime: 0,
    setexit: 0,
    commit: 0,
    finish: 0,
    getlasterror: 0,
    geterrorstring: 0,
    getdiagnostic: 0
};
var studydata = {};

getCourseInfo();

window.onload = function() {
    self.moveTo(0, 0);
    self.resizeTo(window.screen.availWidth, window.screen.availHeight);
};
$(function() {
    
    loadSections();
    
    $("#img_Control").css({ cursor: "pointer", position: "fixed", top: $(window).height() / 2 - 80 })
            .toggle(function() {
                $("iframe").hide();
                $("#dv_menu").animate({ left: '-19%' }, 300, function() { $("iframe").show(); });
                $("#dv_main").animate({ left: '1%', width: '98%' }, 300);
                $(this).attr({ "src": rootpath + "images/al_2.gif", title: "显示菜单" });
            }, function() {
                $("iframe").hide();
                $("#dv_menu").animate({ left: '0' }, 300, function() { $("iframe").show(); });
                $("#dv_main").animate({ left: '20%', width: '79%' }, 300);
                $(this).attr({ "src": rootpath + "images/al_1.gif", title: "隐藏菜单" });
            });
    $("#dialog").dialog({
        autoOpen: false,
        width: 600,
        height: 500,
        modal: false,
        resizable: false
    });

    $("#btn_showresult").bind("click", showResult);
});
function getCourseInfo() {
    $.ajax({
        type: "get",
        url: rootpath + "api/course.ashx",
        data: { type: "getcourseinfo", isedit: 0, courseid: courseid, r: Math.random() },
        dataType: "json",
        async: false,
        success: function(data) {
            directory = rootpath + "data/course/" + data.schoolid + "/" + data.serialnumber + "/";
        }
    });
}
function loadSections() {
    $.ajax({
        type: "get",
        url: rootpath + "api/course.ashx",
        data: { type: "getcoursesection", courseid: courseid, r: Math.random() },
        dataType: "json",
        success: function(data) {
            $(".treemenu").empty();

            var sections = $("<ul/>").appendTo($(".treemenu"));

            $.each(data, function(i, item) {
                var link = "<a href='javascript:;' rel='" + item.indentifier + "'>" + item.title + "</a>";
                if (item.type != "title") {
                    link = "<a href='" + directory + item.href + "' target='main' rel='" + item.indentifier + "'>" + item.title + "</a>";
                }

                var curli = $("<li/>").html(link);

                if (item.parent == "") {
                    sections.append(curli);
                }
                else {
                    var cura = $("a[rel='" + item.parent + "']");
                    var curul = cura.next();

                    if (curul.length == 0) {
                        curul = $("<ul/>");
                        cura.after(curul);
                    }

                    curul.append(curli);
                }

            });

            $("a[rel]").each(function(i) {
                var _this = $(this);
                if (_this.next().length > 0) {
                    $(this).addClass("b");
                }
                else {
                    $(this).addClass("w").click(function() {
                        $(".current").removeClass("current");
                        $(this).addClass("current");
                    });
                }
            });
        }
    });
}

function initResult() {
    result = {
        init: 0,
        getlocation: 0,
        setlocation: 0,
        getsuspenddata: 0,
        setsuspenddata: 0,
        getstatus: 0,
        setstatus: 0,
        getscore: 0,
        setscore: 0,
        gettotaltime: 0,
        getprogress: 0,
        setsessiontime: 0,
        setexit: 0,
        commit: 0,
        finish: 0,
        getlasterror: 0,
        geterrorstring: 0,
        getdiagnostic: 0
    };
}
function showResult() {

    var msg = "<table class=\"resulttable\">";
    msg += "<tr><td>";
    if (result.init > 0) {
        msg += "<span style='color:green;'>当前sco会调用平台的接口初如化函数</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会调用平台的接口初如化函数</span>";
    }
    msg += "</td></tr><tr><td>";
    
    
    if (result.getstatus > 0) {
        msg += "<span style='color:green;'>当前sco会从平台获得了章节的状态</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会从平台获得了章节的状态</span>";
    }
    msg += "</td></tr><tr><td>";
    
    if (result.setstatus > 0) {
        msg += "<span style='color:green;'>当前sco会记录sco的完成状态</span><div style='color:red;background:green'>(status:" + studydata.status + ")</div>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会记录sco的完成状态</span>";
    }
    msg += "</td></tr><tr><td>";
    
    if (result.getlocation > 0) {
        msg += "<span style='color:green;'>当前sco会加载上次退出时的位置</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会加载上次退出时的位置</span>";
    }
    msg += "</td></tr><tr><td>";
    
    if (result.setlocation > 0) {
        msg += "<span style='color:green;'>当前sco会记录上次退出时的位置</span><div>(location:" + studydata.location + ")</div>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会记录上次退出时的位置</span>";
    }
    msg += "</td></tr><tr><td>";
    
    if (result.gettotaltime > 0) {
        msg += "<span style='color:green;'>当前sco会加载课程的完成总时间</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会加载课程的完成总时间</span>";
    }
    msg += "</td></tr><tr><td>";

    if (result.setsessiontime > 0) {
        msg += "<span style='color:green;'>当前sco会记录章节的学习时间</span><div>(sessiontime:" + studydata.sessiontime + ")</div>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会记录章节的学习时间</span>";
    }
    msg += "</td></tr><tr><td>";

    if (result.getscore > 0) {
        msg += "<span style='color:green;'>当前sco会加载章节之前的测试分数</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会加载章节之前的测试分数</span>";
    }

    msg += "</td></tr><tr><td>";
    
    if (result.setscore > 0) {
        msg += "<span style='color:green;'>当前sco会记录章节的测试分数</span><div>(score:" + studydata.score + ")</div>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会记录章节的测试分数</span>";
    }
    msg += "</td></tr><tr><td>";
    
    if (result.getsuspenddata > 0) {
        msg += "<span style='color:green;'>当前sco会加载章节的suspenddata</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会加载章节的suspenddata</span>";
    }

    msg += "</td></tr><tr><td>";
    
    if (result.setsuspenddata > 0) {
        msg += "<span style='color:green;'>当前sco会记录章节的suspenddata</span><div>(suspend_data:" + studydata.suspenddata + ")</div>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会记录章节的suspenddata</span>";
    }

    msg += "</td></tr><tr><td>";

    if (result.commit == 1) {
        msg += "<span style='color:green;'>当前sco会提交章节学习纪录</span>";
    }
    else if (result.commit == 0) {
        msg += "<span style='color:red;'>当前sco不会提交章节学习纪录</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco会多次提交章节学习纪录</span>";
    }

    msg += "</td></tr><tr><td>";

    if (result.finish > 0) {
        msg += "<span style='color:green;'>当前sco会调用平台结束课程学习的函数</span>";
    }
    else {
        msg += "<span style='color:red;'>当前sco不会调用平台结束课程学习的函数</span>";
    }
    msg += "</td></tr></table>";
    
    $("#dialog").html(msg);
    $("#dialog").dialog("open");
}

function API_Calls() {
    this.LMSInitialize = LMSInitialize;
    this.LMSSetValue = LMSSetValue;
    this.LMSGetValue = LMSGetValue;
    this.LMSCommit = LMSCommit;
    this.LMSFinish = LMSFinish;
    this.LMSGetLastError = LMSGetLastError;
    this.LMSGetErrorString = LMSGetErrorString;
    this.LMSGetDiagnostic = LMSGetDiagnostic;
}
var API = new API_Calls();

function LMSInitialize(val) {
    initResult();
    result.init += 1;

    if (val != "") {
        lasterrorcode = 201;
        return false;
    }

    return true;
}
function LMSCommit(val) {
    result.commit += 1;

    if (val != "") {
        lasterrorcode = 201;
        return false;
    }

    return true;
}
function LMSFinish() {
    result.finish += 1;
    showResult();
    return true;
}
function LMSSetValue(name, value) {
    switch (name) {
        case "cmi.core.student_id":
            lasterrorcode = 403
            break;
        case "cmi.core.lesson_location":
            result.setlocation += 1;
            studydata.location = value;
            break;
        case "cmi.core.lesson_status":
            result.setstatus += 1;
            studydata.status = value;
            break;
        case "cmi.core.session_time":
            result.setsessiontime += 1;
            studydata.sessiontime = value;
            break;
        case "cmi.core.lesson_mode":
            lasterrorcode = 403
            break;
        case "cmi.core.score.raw":
            result.setscore += 1;
            studydata.score = value;
            break;
        case "cmi.suspend_data":
            result.setsuspenddata += 1;
            studydata.suspenddata = value;
            break;
        case "cmi.core.lesson_progress":
            result.setprogress += 1;
            studydata.progress = value;
            break
        case "cmi.core.exit":
            result.setexit += 1;
            studydata.exit = value;
            break;
        default:
            break;
    }
    return "true";
}
function LMSGetValue(name) {
    switch (name) {
        case "cmi.core.student_id":
            break;
        case "cmi.core.lesson_location":
            result.getlocation += 1;
            return "80300";
            break;
        case "cmi.core.lesson_status":
            result.getstatus += 1;
            break;
        case "cmi.core.session_time":
            lasterrorcode = 404;
            break;
        case "cmi.core.lesson_mode":
            break;
        case "cmi.core.total_time":
            result.gettotaltime += 1;
            break;
        case "cmi.core.score.raw":
            result.getscore += 1;
            break
        case "cmi.core.lesson_progress":
            result.getprogress += 1;
            break
        case "cmi.suspend_data":
            result.getsuspenddata += 1;
            break
        case "cmi.core.entry":
            break;
        default:
            lasterrorcode = 401;
            break;
    }
    return "";
}
function LMSGetLastError() {
    result.getlasterror += 1;

    var errorCode = lasterrorcode;
    lasterrorcode = 0;

    return errorCode;
}
function LMSGetErrorString(code) {
    result.geterrorstring += 1;
    switch (code) {
        case 0: return "No error";
        case 101: return "General exception";
        case 201: return "Invalid argument error";
        case 202: return "Element cannot have children";
        case 203: return "Element not an array – cannot have count";
        case 301: return "Not initialized";
        case 401: return "Not implemented error";
        case 402: return "Invalid set value, element is a keyword";
        case 403: return "Element is read only";
        case 404: return "Element is write only";
        case 405: return "Incorrect Data Type";
    }
}
function LMSGetDiagnostic(code) {
    result.getdiagnostic += 1;
    return value;
}
        